import numpy as np
def read_outputs( self, last_frame = False,
                        last_cross_section = True ):
    odb = self.attach_results()
    cs_list = set()
    for pload in self.ploadconf.ploads:
        cs_list.add( pload.cross_section )
    if last_cross_section:
        print 'WARNING - reading only last cross section'
        cs_list.add( self.cross_sections[-1] )
        for pload in self.ploadconf.ploads:
            if  pload.cross_section <> None:
                cs_list.add( pload.cross_section )
            else:
                print 'WARNING - pload %s with cross_section = None' \
                      % pload.name
    else:
        for cross_section in self.cross_sections:
            cs_list.add( cross_section )
    for cs in cs_list:
        self.read_outputs_cross_section(\
                    odb = odb,
                    cross_section_index = cs.index,
                    last_frame          = last_frame )
    self.read_axial_load_displ( step_num = 2 )
    self.detach_results( odb )

def old_read_outputs_cross_section( self,
                                    odb,
                                    cross_section_index = -1,
                                    last_frame = False ):
    if cross_section_index == -1:
        cross_section_index = len(self.cross_sections)-1
    cross_section = self.cross_sections[ cross_section_index ]
    setname = 'CROSS_SECTION_%03d' % cross_section.index
    odb_mesh_node_array = odb.rootAssembly.instances['INSTANCECYLINDER'].\
                              nodeSets[setname]
    #
    step_names = odb.steps.keys()
    #
    # reseting values
    nodes = cross_section.nodes
    nodes_dict = {}
    for node in nodes:
        if node == None:
            continue
        nodes_dict[ node.id() ] = node
        for step_name in step_names:
            frmlen = len(odb.steps[ step_name ].frames)
            node.dx[ step_name ] = [None for i in range(frmlen)]
            node.dy[ step_name ] = [None for i in range(frmlen)]
            node.dz[ step_name ] = [None for i in range(frmlen)]
            node.dr[ step_name ] = [None for i in range(frmlen)]
            # for NFORC outputs a starting float value is required
            # because there are more then one nodeLabel value
            #node.fx[ step_name ] = [0. for i in range(frmlen)]
            #node.fy[ step_name ] = [0. for i in range(frmlen)]
            node.fz[ step_name ] = [0.   for i in range(frmlen)]
    # reading new values
    for step_name in step_names:
        frames = odb.steps[ step_name ].frames
        #
        if last_frame:
            frm_list = [ -1 ]
        else:
            frm_list = range( len(frames) )
        for i in frm_list:
            frame = frames[ i ]
            #
            fOut = frame.fieldOutputs['U']
            subfOut = fOut.getSubset(region=odb_mesh_node_array)
            subfOutValues = subfOut.values
            for value in subfOutValues:
                node_id = value.nodeLabel
                #if not node_id in nodes_dict.keys():
                #    continue
                node = nodes_dict[ node_id ]
            #for value in frame.fieldOutputs['NFORC1'].values:
            #    node_id = value.nodeLabel
            #    tmp['NFORC1'][step_name][node_id] = value.data  
            #for value in frame.fieldOutputs['NFORC2'].values:
            #    node_id = value.nodeLabel
            #    tmp['NFORC2'][step_name][node_id] = value.data  
                data = value.data
                dx = data[0]
                dy = data[1] 
                dz = data[2] 
                # this set of operations below could be done in a array
                thetarad = np.arctan2( dy, dx )
                dr  = dx / np.cos( np.deg2rad( thetarad ) ) 
                node.dx[ step_name ][ i ] = dx
                node.dy[ step_name ][ i ] = dy
                node.dz[ step_name ][ i ] = dz
                node.dr[ step_name ][ i ] = dr
                #
                #fx = tmp['NFORC1'][step_name][node_id]
                #
                #fy = tmp['NFORC2'][step_name][node_id]
                #
            fOut = frame.fieldOutputs['NFORC3']
            subfOut = fOut.getSubset(region=odb_mesh_node_array)
            subfOutValues = subfOut.values
            for value in subfOutValues: 
                node_id = value.nodeLabel
                #if not node_id in nodes_dict.keys():
                #    continue
                node = nodes_dict[ node_id ]
                node.fz[ step_name ][ i ] += value.data
                #
                #node.fx[ step_name ] += fx
                #node.fy[ step_name ] += fy

def read_outputs_cross_section( self,
                                odb,
                                cross_section_index = -1,
                                last_frame = False ):
    if cross_section_index == -1:
        cross_section_index = len(self.cross_sections)-1
    cross_section = self.cross_sections[ cross_section_index ]
    setname = 'CROSS_SECTION_%03d' % cross_section.index
    odb_mesh_node_array = odb.rootAssembly.instances['INSTANCECYLINDER'].\
                              nodeSets[setname]
    #
    step_names = odb.steps.keys()
    #
    # reseting values
    nodes = cross_section.nodes
    nodes_dict = {}
    for node in nodes:
        if node == None:
            continue
        nodes_dict[ node.id() ] = node
        for step_name in step_names:
            frmlen = len(odb.steps[ step_name ].frames)
            node.dx[ step_name ] = [None for i in range(frmlen)]
            node.dy[ step_name ] = [None for i in range(frmlen)]
            node.dz[ step_name ] = [None for i in range(frmlen)]
            node.dr[ step_name ] = [None for i in range(frmlen)]
            # for NFORC outputs a starting float value is required
            # because there are more then one nodeLabel value
            #node.fx[ step_name ] = [0. for i in range(frmlen)]
            #node.fy[ step_name ] = [0. for i in range(frmlen)]
            node.fz[ step_name ] = [0.   for i in range(frmlen)]
    # reading new values
    for step_name in step_names:
        frames = odb.steps[ step_name ].frames
        #
        if last_frame:
            frm_list = [ -1 ]
        else:
            frm_list = range( len(frames) )
        for i in frm_list:
            frame = frames[ i ]
            #
            fOut = frame.fieldOutputs['U']
            subfOut = fOut.getSubset(region=odb_mesh_node_array)
            subfOutValues = subfOut.values
            for value in subfOutValues:
                node_id = value.nodeLabel
                #if not node_id in nodes_dict.keys():
                #    continue
                node = nodes_dict[ node_id ]
            #for value in frame.fieldOutputs['NFORC1'].values:
            #    node_id = value.nodeLabel
            #    tmp['NFORC1'][step_name][node_id] = value.data  
            #for value in frame.fieldOutputs['NFORC2'].values:
            #    node_id = value.nodeLabel
            #    tmp['NFORC2'][step_name][node_id] = value.data  
                data = value.data
                dx = data[0]
                dy = data[1] 
                dz = data[2] 
                # this set of operations below could be done in a array
                thetarad = np.arctan2( dy, dx )
                dr  = dx / np.cos( np.deg2rad( thetarad ) ) 
                node.dx[ step_name ][ i ] = dx
                node.dy[ step_name ][ i ] = dy
                node.dz[ step_name ][ i ] = dz
                node.dr[ step_name ][ i ] = dr
                #
                #fx = tmp['NFORC1'][step_name][node_id]
                #
                #fy = tmp['NFORC2'][step_name][node_id]
                #
            fOut = frame.fieldOutputs['NFORC3']
            subfOut = fOut.getSubset(region=odb_mesh_node_array)
            subfOutValues = subfOut.values
            for value in subfOutValues: 
                node_id = value.nodeLabel
                #if not node_id in nodes_dict.keys():
                #    continue
                node = nodes_dict[ node_id ]
                node.fz[ step_name ][ i ] += value.data
                #
                #node.fx[ step_name ] += fx
                #node.fy[ step_name ] += fy
